/**
 *  Copyright (c)  2011-2020 Panguso, Inc.
 *  All rights reserved.
 *
 *  This software is the confidential and proprietary information of Panguso, 
 *  Inc. ("Confidential Information"). You shall not
 *  disclose such Confidential Information and shall use it only in
 *  accordance with the terms of the license agreement you entered into with Panguso.
 */

/*

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.edu.thu.log.tree;

// JAXP
import java.io.IOException;
import java.io.StringReader;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;
import org.xml.sax.InputSource;
import storage.dbhelper.NoiseIdentifyRuleHelper;
import storage.dbhelper.TraceFilterRuleHelper;
import storage.dbobject.DbNoiseIdentifyRule;
import storage.dbobject.DbTraceFilterRule;

/**
 * 
 * @author 2600
 */
public class FromXMLToTree {

	private Element childelement;
	private String minValue = null;
	private String maxValue = null;
	private Map<String, String> operatorMap;
	private String filterType;
/**
 * 
 */
	public FromXMLToTree() {
		operatorMap = new HashMap<String, String>();
		operatorMap.put("and", "1");
		operatorMap.put("or", "2");
		operatorMap.put("not", "3");
		operatorMap.put("smaller", "4");
		operatorMap.put("bigger", "5");
		operatorMap.put("not bigger", "6");
		operatorMap.put("not smaller", "7");
		operatorMap.put("between", "8");
		operatorMap.put("not between", "9");
		operatorMap.put("duration", "10");
		operatorMap.put("equal", "11");
		operatorMap.put("not equal", "12");
		operatorMap.put("equal without case", "13");
		operatorMap.put("not equal without case", "14");
		operatorMap.put("in", "15");
		operatorMap.put("not in", "16");
		operatorMap.put("like", "17");
		operatorMap.put("not like", "18");

		operatorMap.put("first x", "19");
		operatorMap.put("last x", "20");
		operatorMap.put("first x last y", "21");
		operatorMap.put("key events", "22");
		operatorMap.put("sequence", "23");
		operatorMap.put("at least x satisfied", "24");
		operatorMap.put("all satisfied", "25");
		operatorMap.put("constraint", "26");
	}
/**
 * 
 * @param filename parameter
 * @return
 */
	public Element readXML(String filename) {
		// parse the XML as a W3C Document
		// 浣跨敤SAXBuilder瀵硅薄
		// System.out.println("readXML filename is:" + filename);
		String noiseRuleString = "";

		NoiseIdentifyRuleHelper noiseHelper = new NoiseIdentifyRuleHelper();
		List<DbNoiseIdentifyRule> noiseRules = noiseHelper.getNoiseRules(
				DbNoiseIdentifyRule.CL_NAME, filename);
		DbNoiseIdentifyRule noiseRule;
		// System.out.println("noiseRules.size():" + noiseRules.size());
		if (noiseRules.size() > 0) {

			noiseRule = noiseRules.get(0);
			noiseRuleString = noiseRule.getNoiseRule();
		}
		System.out.println(noiseRuleString);
		SAXBuilder sax = new SAXBuilder();
		StringReader sr = new StringReader(noiseRuleString);
		InputSource source = new InputSource(sr);
		// System.out.println("noiseRuleString:" + noiseRuleString);
		// 鍒版暟鎹簱璇诲彇filename鐨剎ml鏂囦欢

		Document doc = null;
		Element root = null;
		try {
			// doc = sax.build("E:\\treerule4.xml");
			doc = sax.build(source);
			Element xmlroot = doc.getRootElement(); // 寰楀埌鏍硅妭鐐?

			if (xmlroot.getChild("maxValue") != null) {
				Element max = xmlroot.getChild("maxValue");
				maxValue = max.getAttributeValue("value");
			}
			if (xmlroot.getChild("minValue") != null) {
				Element min = xmlroot.getChild("minValue");
				minValue = min.getAttributeValue("value");
			}

			// System.out.println("minValue is:" + getMinValue() +
			// " maxValue is:" + getMaxValue());

			Element treeroot = xmlroot.getChild("div");
			// if (treeroot.getAttributeValue("class").equals("dtree")) {
			// System.out.println("get tree root file!");
			// }

			root = (Element) XPath.selectSingleNode(treeroot,
					"./div[@id]/div[@class=\"dTreeNode\"]");

		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return root;
	}
/**
 * 
 * @param filename parameter
 * @return
 */
	public Element readTraceXML(String filename) {
		// parse the XML as a W3C Document
		// 浣跨敤SAXBuilder瀵硅薄9
		// System.out.println("readXML filename is:" + filename);
		String traceRuleString = "";

		TraceFilterRuleHelper traceHelper = new TraceFilterRuleHelper();
		List<DbTraceFilterRule> traceRules = traceHelper.getTraceFilterRules(
				DbTraceFilterRule.CL_NAME, filename);
		DbTraceFilterRule traceRule;
		// System.out.println(traceRules.size());

		if (traceRules.size() > 0) {
			traceRule = traceRules.get(0);
			traceRuleString = traceRule.getTraceFilterRule();
			// System.out.println("hohoho:"+traceRuleString);
			try {
				traceRuleString = URLDecoder.decode(traceRuleString, "UTF-8");
			} catch (Exception e) {
				traceRuleString = "";
			}

			filterType = traceRule.getFilterType();
		}

		SAXBuilder sax = new SAXBuilder();
		StringReader sr = new StringReader(traceRuleString);
		InputSource source = new InputSource(sr);

		// 鍒版暟鎹簱璇诲彇filename鐨剎ml鏂囦欢

		Document doc = null;
		Element root = null;
		try {
			// doc = sax.build("E:\\treerule4.xml");
			doc = sax.build(source);
			Element xmlroot = doc.getRootElement(); // 寰楀埌鏍硅妭鐐?

			if (xmlroot.getChild("maxValue") != null) {
				Element max = xmlroot.getChild("maxValue");
				maxValue = max.getAttributeValue("value");
			}
			if (xmlroot.getChild("minValue") != null) {
				Element min = xmlroot.getChild("minValue");
				minValue = min.getAttributeValue("value");
			}

			// System.out.println("minValue is:" + getMinValue() +
			// " maxValue is:" + getMaxValue());

			Element treeroot = xmlroot.getChild("div");
			// if (treeroot.getAttributeValue("class").equals("dtree")) {
			// System.out.println("get tree root file!");
			// }

			root = (Element) XPath.selectSingleNode(treeroot,
					"./div[@id]/div[@class=\"dTreeNode\"]");

		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return root;
	}
/**
 * 
 * @param node parameter
 * @return
 * @throws JDOMException exception
 */
	public ManyTreeNode createTree(Element node) throws JDOMException {
		int flag = 0;
		Element p = (Element) XPath.selectSingleNode(node,
				"./a[@class=\"node\"]");
		String nodename;
		p.getText();
		// System.out.println("treenode name!");
		String type = p.getAttributeValue("type");
		String id = p.getAttributeValue("id").substring(2);
		// System.out.println("create Tree!");
		// System.out.println("nodename:" + nodename + "  type:" + type +
		// "  id:" + id);

		if (type.equals("operator")) {
			nodename = operatorMap.get(p.getText());
		} else {
			nodename = p.getText();
		}

		XMLNode xmlnode = new XMLNode(id, nodename, type);

		ArrayList<ManyTreeNode> childList = new ArrayList<ManyTreeNode>();
		ManyTreeNode rootNode = new ManyTreeNode(xmlnode, childList);

		// 鍒涘缓瀛愯妭鐐瑰垪琛?
		List neighbourWithChild = node.getParentElement().getChildren();
		String currentid = p.getAttributeValue("id").substring(2);
		Element e = null;
		for (int i = 0; i < neighbourWithChild.size(); i++) {
			e = (Element) neighbourWithChild.get(i);
			// 鎵惧埌瀹冪殑瀛╁瓙鑺傜偣闆唀
			if (e.getAttributeValue("id") != null
					&& e.getAttributeValue("id").substring(2).equals(currentid)) {
				childelement = e;

				List childNodeList = childelement.getChildren();
				if (childNodeList.isEmpty()) {
					// System.out.println("child size is:" +
					// childNodeList.size());
					return rootNode;
				} else {
					Element c = null;
					for (int j = 0; j < childNodeList.size(); j++) {
						c = (Element) childNodeList.get(j);
						if (c.getAttributeValue("class").equals("dTreeNode")) {
							childList.add(createTree(c));
						}
					}
					rootNode.setChildList(childList);
				}
			}
		}
		return rootNode;
	}

	/**
	 * @return the minValue
	 */
	public String getMinValue() {
		return minValue;
	}

	/**
	 * @param minValue
	 *            the minValue to set
	 */
	public void setMinValue(String minValue) {
		this.minValue = minValue;
	}

	/**
	 * @return the maxValue
	 */
	public String getMaxValue() {
		return maxValue;
	}

	/**
	 * @param maxValue
	 *            the maxValue to set
	 */
	public void setMaxValue(String maxValue) {
		this.maxValue = maxValue;
	}

	/**
	 * @return the filterType
	 */
	public String getFilterType() {
		return filterType;
	}

	/**
	 * @param filterType
	 *            the filterType to set
	 */
	public void setFilterType(String filterType) {
		this.filterType = filterType;
	}
}
